const dbHelper = require('./dbHelper');
const student = require('../model/student');

let UserServices = {
    // 获取用户列表，执行自定义sql语句
    listUsers: (callback) => {
        // 验证数据库连接
        // dbHelper.CheckConnect();
        dbHelper.Query('SELECT * FROM sys_user', (result) => {
            callback(result);
        });
    },
    // 获取用户列表
    studentList: async callback => {
        const list = await student.findAll({
            // 属性转换
            // attributes: ['id',['name','nickName'],'sex']
        });
        callback(list);
    },
    // 添加用户
    addStudent: async (params, callback) => {
        const user = await student.create(params).catch(reason => {
            callback("保存失败！错误原因：" + reason);
        });

        if (user !== undefined)
            callback(user);
    },
    // 删除用户
    deleteStudent: async (params, callback) => {
        const count = await student.destroy({
            where: {
                studentId: params.studentId
            }
        }).catch(reason => {
            callback("删除失败！错误原因：" + reason);
        });

        if (count > 0)
            callback("删除成功！");
        else
            callback("数据不存在");
    },
    // 修改用户
    putStudent: async (params, callback) => {
        const user = await student.update(params, {
            where: {
                studentId: params.studentId
            }
        }).catch(reason => {
            callback("修改失败！错误原因：" + reason);
        });

        if (user !== undefined)
            callback("修改成功！");
    },
    // id获取用户
    getStudentById: async (params, callback) => {
        const user = await student.findAll({
            where: {
                studentId: params.studentId
            }
        }).catch(reason => {
            callback("获取失败！错误原因：" + reason);
        });

        if (user !== undefined)
            callback(user);
    }
}

module.exports = UserServices;